﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using FrameworkQ.ObjectSpace;

public partial class _Default : System.Web.UI.Page
{
    List<FoodValue> _foodValues = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        FoodValueFacade facade = new FoodValueFacade();
        //facade.BuildComplexFoodDB();
        if (_foodValues == null)
        {
            _foodValues = Manager.GetAll();
        }

        if (cboFoodItem.Items.Count <= 0)
        {
            foreach (FoodValue val in _foodValues)
            {
                cboFoodItem.Items.Add(new ListItem(val.Name, val.FoodCode.ToString()));
            }
        }

        cboConfigs.Items.Clear();
        List<ComplexFood> cfs = facade.GetConfigs();
        foreach (ComplexFood cf in cfs)
        {
            cboConfigs.Items.Add(new ListItem(cf.FoodName, cf.ComplexFoodID.ToString()));
        }
        RenderModel();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int foodCode = 0;
        int.TryParse(cboFoodItem.SelectedValue, out foodCode);
        int amount = 0;
        int.TryParse (txtAmount.Text, out amount);
        if (foodCode > 0 && amount> 0)
        {
            SelectionModel model = Manager.GetSelectionModel();
            SelectionItem item = null;
            foreach (SelectionItem si in model.SelectedItems)
            {
                if (si.FoodCode == foodCode)
                {
                    item = si;
                }
            }
            if (item == null)
            {
                item = new SelectionItem() { FoodCode = foodCode, Amount = amount };
                model.SelectedItems.Add(item);
            }
            else
            {
                item.Amount += amount;
            }
            
            Manager.SetSelectionModel(model);
            RenderModel();
        }
    }

    private void RenderModel()
    {
        SelectionModel model = Manager.GetSelectionModel();
        GridView1.DataSource = model.GetCalculatedFoodValues();
        GridView1.DataBind();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int foodCode = 0;
        int.TryParse(e.CommandArgument.ToString(), out foodCode);

        if (foodCode > 0)
        {
            SelectionModel model = Manager.GetSelectionModel();
            if (e.CommandName == "Delete")
            {
                SelectionItem itemFound = null;
                foreach (SelectionItem item in model.SelectedItems)
                {
                    if (item.FoodCode == foodCode)
                    {
                        itemFound = item;
                    }
                }
                if (itemFound != null)
                {
                    model.SelectedItems.Remove(itemFound);
                    Manager.SetSelectionModel(model);
                    Response.Redirect("~/Default.aspx");
                }
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row != null)
        {
            HiddenField hid = e.Row.FindControl("hidFoodCode") as HiddenField;
            LinkButton lnkX = e.Row.FindControl("lnkDelete") as LinkButton;
            if (hid != null)
            {
                FoodValueDisplay disp = e.Row.DataItem as FoodValueDisplay;
                if (disp != null)
                {
                    hid.Value = disp.FoodCode.ToString();
                    if (lnkX != null)
                    {
                        lnkX.CommandArgument = disp.FoodCode.ToString();
                        lnkX.CommandName = "Delete";
                    }
                }
            }
        }
    }
    
    protected void lnkClear_Click(object sender, EventArgs e)
    {
        SelectionModel model = Manager.GetSelectionModel();
        model.SelectedItems.Clear();
        Manager.SetSelectionModel(model);
        RenderModel();
    }
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Download.aspx");
    }

    protected void lnkSaveConfig_Click(object sender, EventArgs e)
    {
        FoodValueFacade facade = new FoodValueFacade();
        facade.SaveModel(Manager.GetSelectionModel(), txtSaveConfig.Text);
    }

    protected void lnkAddConfig_Click(object sender, EventArgs e)
    {
        FoodValueFacade facade = new FoodValueFacade();
        string modelName = cboConfigs.SelectedItem.Text;

        SelectionModel model = facade.LoadModel(modelName);
        Manager.AddToSelectionModel(model);
        RenderModel();
    }
}
